﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Billing.Utilities;
using System.Data;
using Billing.Controls;
using System.Windows.Forms.Integration;
using System.Drawing.Printing;
using System.Configuration;

namespace Billing.Windows
{
    public partial class SearchCakes : Window
    {
        DataTable dtSearchGrid = null;
        string cakeBillNO = string.Empty;

        public SearchCakes()
        {
            InitializeComponent();
        }

        private void Window_PreviewKeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key == Key.Escape)
                {
                    this.Close();
                }
                else if (e.Key == Key.F1)
                {
                    byBillNoBrdr.Visibility = Visibility.Visible;
                    byDeliveryDateBrdr.Visibility = Visibility.Collapsed;
                    byPhoneNo.Visibility = Visibility.Collapsed;
                    msg.Text = "Enter Cake Bill No and Press Enter";
                    cakebillNo.Clear();
                    cakebillNo.Focus();
                }
                else if (e.Key == Key.F2)
                {
                    byBillNoBrdr.Visibility = Visibility.Collapsed;
                    byDeliveryDateBrdr.Visibility = Visibility.Visible;
                    byPhoneNo.Visibility = Visibility.Collapsed;
                    printGrid.Visibility = Visibility.Collapsed;
                    msg.Text = "Select valid Date and Press G";
                    deliverydatepicker.SetFocusOnDate();
                }
                else if (e.Key == Key.F3)
                {
                    byBillNoBrdr.Visibility = Visibility.Collapsed;
                    byDeliveryDateBrdr.Visibility = Visibility.Collapsed;
                    byPhoneNo.Visibility = Visibility.Visible;
                    printGridByMobile.Visibility = Visibility.Collapsed;
                    msg.Text = "Enter Customer Phone Number and Press Enter";
                    customerPhoneNumber.Clear();
                    customerPhoneNumber.Focus();
                }
                else if (e.Key == Key.Enter && byBillNoBrdr.Visibility == Visibility.Visible && cakebillNo.Text != string.Empty)
                {
                    DataTable dt = Database.GetCakeDetails(cakebillNo.Text);
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        msg.Text = "Details for Bill # " + cakebillNo.Text + " are filled below.";
                        cakeBillNO = cakebillNo.Text;
                        customerName.Text = dt.Rows[0]["CustomerName"].ToString();
                        phoneNo.Text = dt.Rows[0]["PhoneNo"].ToString();
                        bookedDate.Text = DateTime.Parse(dt.Rows[0]["BookedDate"].ToString()).ToString("dd-MMM-yy  hh:mm:tt");
                        deliveryDate.Text = DateTime.Parse(dt.Rows[0]["DeliveryDate"].ToString()).ToString("dd-MMM-yy") + "  " + dt.Rows[0]["DeliveryTime"].ToString();
                        shape.Text = dt.Rows[0]["CakeShape"].ToString();
                        albumModel.Text = dt.Rows[0]["AlbumModelDesc"].ToString();
                        message.Text = dt.Rows[0]["Message"].ToString();
                        description.Text = dt.Rows[0]["Desc"].ToString();
                        flavour.Text = dt.Rows[0]["Flavour"].ToString();
                        qty.Text = dt.Rows[0]["Qty"].ToString();
                        totalAmt.Text = dt.Rows[0]["NetAmount"].ToString();
                        advance.Text = dt.Rows[0]["Advance"].ToString();
                        balance.Text = dt.Rows[0]["Balance"].ToString();
                        cakeDelivery.Text = dt.Rows[0]["IsCakeDelivered"].ToString();
                        extra.Text = dt.Rows[0]["ExtraPercentage"].ToString();
                        extraValue.Content = dt.Rows[0]["ExtraValue"].ToString();
                        discount.Text = dt.Rows[0]["DiscountByCost"].ToString();
                        isChefCopyPrinted.Text = dt.Rows[0]["IsChefCopyPrinted"].ToString();
                        printCake.Visibility = Visibility.Visible;
                        photo.Text = dt.Rows[0]["PhotoSheet"].ToString();
                        billNo.Text = dt.Rows[0]["BillNo"].ToString();
                    }
                    else
                    {
                        msg.Text = "Bill # Doesn't Exists";
                        customerName.Text = string.Empty;
                        phoneNo.Text = string.Empty;
                        bookedDate.Text = string.Empty;
                        deliveryDate.Text = string.Empty;
                        shape.Text = string.Empty;
                        albumModel.Text = string.Empty;
                        message.Text = string.Empty;
                        description.Text = string.Empty;
                        flavour.Text = string.Empty;
                        qty.Text = string.Empty;
                        totalAmt.Text = string.Empty;
                        advance.Text = string.Empty;
                        balance.Text = string.Empty;
                        extra.Text = string.Empty;
                        cakeDelivery.Text = string.Empty;
                        cakebillNo.Text = cakeBillNO;
                        extraValue.Content = string.Empty;
                        discount.Text = string.Empty;
                        printCake.Visibility = Visibility.Collapsed;
                        cakebillNo.Clear();
                        cakebillNo.Focus();
                        isChefCopyPrinted.Text = string.Empty;
                        photo.Text = string.Empty;
                        billNo.Text = string.Empty;
                    }
                }
                else if (e.Key == Key.P && byBillNoBrdr.Visibility == Visibility.Visible)
                {
                    if (isChefCopyPrinted.Text != "True")
                    {
                        Database.SetCakeChefCopyPrinted(cakeBillNO);

                        if (ConfigurationManager.AppSettings["PrinterType"] != null && ConfigurationManager.AppSettings["PrinterType"].ToString() == "new")
                        {
                            NewCakePrinter form = new NewCakePrinter(cakeBillNO,
                                    bookedDate.Text,
                                    deliveryDate.Text,
                                    customerName.Text,
                                    phoneNo.Text,
                                    shape.Text,
                                    albumModel.Text,
                                    description.Text,
                                    message.Text,
                                    flavour.Text,
                                    qty.Text,
                                    extraValue.Content.ToString(),
                                    totalAmt.Text,
                                    advance.Text,
                                    balance.Text,
                                    discount.Text,
                                    true,
                                    photo.Text,
                                    billNo.Text);
                            form.printDocument1.PrintController = new StandardPrintController();
                            form.printDocument1.Print();
                            form.Dispose();
                        }
                        else
                        {
                            OldCakePrinter form = new OldCakePrinter(cakeBillNO,
                                    bookedDate.Text,
                                    deliveryDate.Text,
                                    customerName.Text,
                                    phoneNo.Text,
                                    shape.Text,
                                    albumModel.Text,
                                    description.Text,
                                    message.Text,
                                    flavour.Text,
                                    qty.Text,
                                    extraValue.Content.ToString(),
                                    totalAmt.Text,
                                    advance.Text,
                                    balance.Text,
                                    discount.Text,
                                    true,
                                    photo.Text,
                                    billNo.Text);
                            form.printDocument1.PrintController = new StandardPrintController();
                            form.printDocument1.Print();
                        }
                    }
                    cakebillNo.Text.Replace("P", string.Empty);
                }
                else if (e.Key == Key.G && byDeliveryDateBrdr.Visibility == Visibility.Visible)
                {
                    dtSearchGrid = Database.GetAllCakesByDeliveryDate(new DateTime(int.Parse(deliverydatepicker.nudYear.txtBoxValue.Text), int.Parse(deliverydatepicker.nudMonth.txtBoxValue.Text), int.Parse(deliverydatepicker.nudDate.txtBoxValue.Text)));
                    lbCakesGrid.Items.Clear();

                    if (dtSearchGrid != null && dtSearchGrid.Rows.Count > 0)
                    {
                        msg.Text = "Details are filled.";
                        printGrid.Visibility = Visibility.Visible;
                        foreach (DataRow row in dtSearchGrid.Rows)
                        {
                            CakeItemControl ctrl = new CakeItemControl();
                            ctrl.lblBillNo.Content = row["CakeNo"].ToString();
                            ctrl.lblCustomerName.Content = row["CustomerName"].ToString();
                            ctrl.lblPhoneNo.Content = row["PhoneNo"].ToString();
                            ctrl.lblDeliveryDate.Content = DateTime.Parse(row["DeliveryDate"].ToString()).ToString("dd-MMM-yy") + " " + row["DeliveryTime"].ToString();
                            ctrl.lblFlavour.Content = row["Flavour"].ToString();
                            if (row["AlbumModelDesc"].ToString() != string.Empty)
                                ctrl.lblShapeOrAlbumModel.Content = row["AlbumModelDesc"].ToString();
                            else
                                ctrl.lblShapeOrAlbumModel.Content = row["CakeShape"].ToString();
                            ctrl.lblQty.Content = row["Qty"].ToString();
                            ctrl.lblAmount.Content = row["NetAmount"].ToString();
                            ctrl.lblBalance.Content = row["Balance"].ToString();
                            ctrl.lblDelivered.Content = row["IsCakeDelivered"].ToString();
                            ctrl.lblChefCopy.Content = row["IsChefCopyPrinted"].ToString();
                            ctrl.lblPhoto.Content = row["PhotoSheet"].ToString();
                            ctrl.lblActBillNo.Content = row["BillNo"].ToString();
                            lbCakesGrid.Items.Add(ctrl);
                        }
                    }
                    else
                    {
                        msg.Text = "No Details Available for the Selected Criteria.";
                        printGrid.Visibility = Visibility.Collapsed;
                    }
                }
                else if (e.Key == Key.P && byDeliveryDateBrdr.Visibility == Visibility.Visible && printGrid.Visibility == Visibility.Visible)
                {
                    foreach (DataRow row in dtSearchGrid.Rows)
                    {
                        if (row["IsChefCopyPrinted"].ToString() != "True")
                        {
                            Database.SetCakeChefCopyPrinted(row["CakeNo"].ToString());
                            if (ConfigurationManager.AppSettings["PrinterType"] != null && ConfigurationManager.AppSettings["PrinterType"].ToString() == "new")
                            {
                                NewCakePrinter form = new NewCakePrinter(row["CakeNo"].ToString(),
                                    DateTime.Parse(row["BookedDate"].ToString()).ToString("dd-MMM-yy  hh:mm:tt"),
                                    DateTime.Parse(row["DeliveryDate"].ToString()).ToString("dd-MMM-yy") + " " + row["DeliveryTime"].ToString(),
                                    row["CustomerName"].ToString(),
                                    row["PhoneNo"].ToString(),
                                    row["CakeShape"].ToString(),
                                    row["AlbumModelDesc"].ToString(),
                                    row["Desc"].ToString(),
                                    row["Message"].ToString(),
                                    row["Flavour"].ToString(),
                                    row["Qty"].ToString(),
                                    row["ExtraValue"].ToString(),
                                    row["NetAmount"].ToString(),
                                    row["Advance"].ToString(),
                                    row["Balance"].ToString(),
                                    row["DiscountByCost"].ToString(),
                                    true,
                                    row["PhotoSheet"].ToString(),
                                    row["BillNo"].ToString());
                                form.printDocument1.PrintController = new StandardPrintController();
                                form.printDocument1.Print();
                            }
                            else
                            {
                                OldCakePrinter form = new OldCakePrinter(row["CakeNo"].ToString(),
                                    DateTime.Parse(row["BookedDate"].ToString()).ToString("dd-MMM-yy  hh:mm:tt"),
                                    DateTime.Parse(row["DeliveryDate"].ToString()).ToString("dd-MMM-yy") + " " + row["DeliveryTime"].ToString(),
                                    row["CustomerName"].ToString(),
                                    row["PhoneNo"].ToString(),
                                    row["CakeShape"].ToString(),
                                    row["AlbumModelDesc"].ToString(),
                                    row["Desc"].ToString(),
                                    row["Message"].ToString(),
                                    row["Flavour"].ToString(),
                                    row["Qty"].ToString(),
                                    row["ExtraValue"].ToString(),
                                    row["NetAmount"].ToString(),
                                    row["Advance"].ToString(),
                                    row["Balance"].ToString(),
                                    row["DiscountByCost"].ToString(),
                                    true,
                                    row["PhotoSheet"].ToString(),
                                    row["BillNo"].ToString());
                                form.printDocument1.PrintController = new StandardPrintController();
                                form.printDocument1.Print();
                            }
                        }
                    }
                }
                else if (e.Key == Key.Enter && byPhoneNo.Visibility == Visibility.Visible && customerPhoneNumber.Text != string.Empty)
                {
                    dtSearchGrid = Database.GetAllCakesByPhoneNo(customerPhoneNumber.Text);
                    lbCakesGridByMobile.Items.Clear();

                    if (dtSearchGrid != null && dtSearchGrid.Rows.Count > 0)
                    {
                        msg.Text = "Details are filled.";
                        printGridByMobile.Visibility = Visibility.Visible;
                        foreach (DataRow row in dtSearchGrid.Rows)
                        {
                            CakeItemControl ctrl = new CakeItemControl();
                            ctrl.lblBillNo.Content = row["CakeNo"].ToString();
                            ctrl.lblCustomerName.Content = row["CustomerName"].ToString();
                            ctrl.lblPhoneNo.Content = row["PhoneNo"].ToString();
                            ctrl.lblDeliveryDate.Content = DateTime.Parse(row["DeliveryDate"].ToString()).ToString("dd-MMM-yy") + " " + row["DeliveryTime"].ToString();
                            ctrl.lblFlavour.Content = row["Flavour"].ToString();
                            if (row["AlbumModelDesc"].ToString() != string.Empty)
                                ctrl.lblShapeOrAlbumModel.Content = row["AlbumModelDesc"].ToString();
                            else
                                ctrl.lblShapeOrAlbumModel.Content = row["CakeShape"].ToString();
                            ctrl.lblQty.Content = row["Qty"].ToString();
                            ctrl.lblAmount.Content = row["NetAmount"].ToString();
                            ctrl.lblBalance.Content = row["Balance"].ToString();
                            ctrl.lblDelivered.Content = row["IsCakeDelivered"].ToString();
                            ctrl.lblPhoto.Content = row["PhotoSheet"].ToString();
                            ctrl.lblActBillNo.Content = row["BillNo"].ToString();
                            lbCakesGridByMobile.Items.Add(ctrl);
                        }
                    }
                    else
                    {
                        msg.Text = "No Details Available for the Selected Criteria.";
                        printGridByMobile.Visibility = Visibility.Collapsed;
                    }
                }
                else if (e.Key == Key.P && byPhoneNo.Visibility == Visibility.Visible && printGridByMobile.Visibility == Visibility.Visible)
                {
                    foreach (DataRow row in dtSearchGrid.Rows)
                    {
                        if (row["IsChefCopyPrinted"].ToString() != "True")
                        {
                            Database.SetCakeChefCopyPrinted(row["CakeNo"].ToString());
                            if (ConfigurationManager.AppSettings["PrinterType"] != null && ConfigurationManager.AppSettings["PrinterType"].ToString() == "new")
                            {
                                NewCakePrinter form = new NewCakePrinter(row["CakeNo"].ToString(),
                                    DateTime.Parse(row["BookedDate"].ToString()).ToString("dd-MMM-yy  hh:mm:tt"),
                                    DateTime.Parse(row["DeliveryDate"].ToString()).ToString("dd-MMM-yy") + " " + row["DeliveryTime"].ToString(),
                                    row["CustomerName"].ToString(),
                                    row["PhoneNo"].ToString(),
                                    row["CakeShape"].ToString(),
                                    row["AlbumModelDesc"].ToString(),
                                    row["Desc"].ToString(),
                                    row["Message"].ToString(),
                                    row["Flavour"].ToString(),
                                    row["Qty"].ToString(),
                                    row["ExtraValue"].ToString(),
                                    row["NetAmount"].ToString(),
                                    row["Advance"].ToString(),
                                    row["Balance"].ToString(),
                                    row["DiscountByCost"].ToString(),
                                    true,
                                    row["PhotoSheet"].ToString(),
                                    row["BillNo"].ToString());
                                form.printDocument1.PrintController = new StandardPrintController();
                                form.printDocument1.Print();
                            }
                            else
                            {
                                OldCakePrinter form = new OldCakePrinter(row["CakeNo"].ToString(),
                                    DateTime.Parse(row["BookedDate"].ToString()).ToString("dd-MMM-yy  hh:mm:tt"),
                                    DateTime.Parse(row["DeliveryDate"].ToString()).ToString("dd-MMM-yy") + " " + row["DeliveryTime"].ToString(),
                                    row["CustomerName"].ToString(),
                                    row["PhoneNo"].ToString(),
                                    row["CakeShape"].ToString(),
                                    row["AlbumModelDesc"].ToString(),
                                    row["Desc"].ToString(),
                                    row["Message"].ToString(),
                                    row["Flavour"].ToString(),
                                    row["Qty"].ToString(),
                                    row["ExtraValue"].ToString(),
                                    row["NetAmount"].ToString(),
                                    row["Advance"].ToString(),
                                    row["Balance"].ToString(),
                                    row["DiscountByCost"].ToString(),
                                    true,
                                    row["PhotoSheet"].ToString(),
                                    row["BillNo"].ToString());
                                form.printDocument1.PrintController = new StandardPrintController();
                                form.printDocument1.Print();
                            }
                        }
                    }
                    customerPhoneNumber.Text = string.Empty;
                }
            }
            catch (Exception) { }
        }
    }
}
